package com.yksj.healthtalk.ui.news;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 
 * 新闻评论
 * @author zhao
 *
 */
public class NewsCommenActivity extends BaseFragmentActivity implements OnClickListener{
	String newsId;
	String userId;
	String replyID;//回复id
	String titleName;
	EditText mEditText;
	TextView textView;
	private String content;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.news_comment_layout);
		userId = getIntent().getStringExtra("userId");
		newsId = getIntent().getStringExtra("newsId");
		replyID = getIntent().getStringExtra("replyId");
		titleName = getIntent().getStringExtra("title");
		initView();
	}  
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			setResult(RESULT_CANCELED);
			finish();
			break;
		case R.id.title_right2:
			content = mEditText.getEditableText().toString();
			if("".equals(content.trim()))return;
			if(!getIntent().hasExtra("server")){
				onComment();
			}else{
				onCommentToServer();
			}
			break;
		}
	}
	
	
	private void onCommentToServer() {
		HttpRestClient.doHttpServerPLunNerId(userId,newsId,content,"saveCenterMerchantNewsReply",new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, String content) {
				if("1".equals(content)){
					mEditText.setText(null);
					showToast(R.string.comment_ok);
				}else{
					showToast(R.string.comment_fail);
				}
				if(!isFinishing()){
					setResult(RESULT_OK);
					finish();
				}
			}
		});
		
	}

	/**
	 * 评论提交
	 */
	private void onComment(){
		HttpRestClient.doHttpNewsComment(newsId, userId, replyID, content,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				if("1".equals(content)){
					mEditText.setText(null);
					showToast(R.string.comment_ok);
				}else{
					showToast(R.string.comment_fail);
				}
				if(!isFinishing()){
					setResult(RESULT_OK);
					finish();
				}
			}
		});
	}
	
	private void showToast(int id){
		ToastUtil.showShort(this,id);
	}
	
	private void initView(){
		initTitle();
		
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setText("确定");
		titleRightBtn2.setVisibility(View.VISIBLE);
		mEditText = (EditText)findViewById(R.id.commentEdit);
		textView  = (TextView)findViewById(R.id.number_txt);
		titleTextV.setText("评论");
		
		mEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				textView.setText(s.length()+"/200");
			}
		});
	}
}
